package ee.maoinvest.frontend.controller.command.regularreadingscommands;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import ee.maoinvest.ErrorMessage;
import ee.maoinvest.backend.model.data.RegularReading;
import ee.maoinvest.backend.services.RegularReadingsService;
import ee.maoinvest.backend.services.implementations.RegularReadingsServiceImpl;
import ee.maoinvest.frontend.controller.command.Command;
import ee.maoinvest.frontend.controller.formstringdataconverters.regularreadingsforms.StartNewRegularReadingFormConverter;
import ee.maoinvest.frontend.controller.requestprocessors.RegularReadingsFormsRequestProcessor;

public class StartNewRegularReadingCommand implements Command
{
	private Logger logger = Logger.getLogger(StartNewRegularReadingCommand.class);
	private int operationResult = 0;
	private final int OPERATION_SUCCESSFUL = 1;
	private final int OPERATION_UNSUCCESSFUL = 0;
	private final int PROBABLY_A_DATABASE_ERROR = -1;
	private int THERE_ARE_NO_ELECTRICITY_METERS_IN_DB_TO_TAKE_PART_IN_REGULAR_READING = -2;
	
	
	@Override
	public int executeCommand(HttpServletRequest request,
			HttpServletResponse response) 
	{
		try 
		{
			RegularReadingsFormsRequestProcessor regularReadingsFormsRequestProcessor = null;
			StartNewRegularReadingFormConverter startNewRegularReadingFormConverter = null;
			RegularReading regularReading = null;
			RegularReadingsService regularReadingsServiceImpl = null;
			
			regularReadingsFormsRequestProcessor = new RegularReadingsFormsRequestProcessor();
			startNewRegularReadingFormConverter = 
					regularReadingsFormsRequestProcessor.fillStartNewRegularReadingWithRequestData(request);
			regularReading = startNewRegularReadingFormConverter.convertToModelData();
			boolean dataIsOKAfterFormatting = startNewRegularReadingFormConverter.getIsDataAfterFormattingOK();
			
			if (dataIsOKAfterFormatting)
			{
				int wasInsertionToDatabaseSuccessful = 0;
				regularReadingsServiceImpl = new RegularReadingsServiceImpl();
				
				wasInsertionToDatabaseSuccessful = regularReadingsServiceImpl.startNewRegularReading(regularReading, request);
	
				
				if (wasInsertionToDatabaseSuccessful == OPERATION_SUCCESSFUL)
				{
					request.setAttribute("startNewRegularReadingFormConverter", startNewRegularReadingFormConverter);
					operationResult = OPERATION_SUCCESSFUL;
				}
				else if (wasInsertionToDatabaseSuccessful == THERE_ARE_NO_ELECTRICITY_METERS_IN_DB_TO_TAKE_PART_IN_REGULAR_READING)
				{
					request.setAttribute("startNewRegularReadingFormConverter", startNewRegularReadingFormConverter);
					request.setAttribute("startingNewRegularReadingWasUnsuccessful", ErrorMessage.E0022_THERE_ARE_NO_ELECTRICITY_METERS_TO_START_REGULAR_READINGS_WITH);
					operationResult = OPERATION_UNSUCCESSFUL;
					return operationResult;
				}
				else
				{
					request.setAttribute("startNewRegularReadingFormConverter", startNewRegularReadingFormConverter);
					request.setAttribute("startingNewRegularReadingWasUnsuccessful", ErrorMessage.E0021_ERROR_WHILE_TRYING_TO_START_NEW_REGULAR_READING); 
					operationResult = OPERATION_UNSUCCESSFUL;
				}
			}
			else
			{
				request.setAttribute("startNewRegularReadingFormConverter", startNewRegularReadingFormConverter);
				operationResult = OPERATION_UNSUCCESSFUL;
			}
				
		}
		catch (Exception e) 
		{
			e.printStackTrace();
			logger.error(e.getMessage());
			return PROBABLY_A_DATABASE_ERROR;
		}
		
		return operationResult;
	}
}
